//
//  ViewController.m
//  20-XML-SAXAndDOM
//
//  Created by GivenBmli on 16/8/10.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "XMLReader.h"

@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic ,strong)NSMutableArray *stuArray;
@property (nonatomic ,copy)NSString *currentElementName;
@property (nonatomic ,strong)NSMutableArray *domStuArray;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    将XML结构,转换成Dictionary
    NSString *xmlDataPath =  [[NSBundle mainBundle]pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlDataPath];
     NSError *erro = nil;
   NSDictionary *xmlDic = [XMLReader dictionaryForXMLData:xmlData error:&erro];
    NSLog(@"%@",xmlDic);
    
    
}
// SOAP 协议
// XML 分为两种解析方式
// 第一种逐行解析,-- 使用系统的解析器NSXMLParser
- (IBAction)saxParse:(id)sender {
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmParser = [[NSXMLParser alloc]initWithData:xmlData];
    xmParser.delegate = self;
    [xmParser parse];
}
// 1.解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 2.遇到节点头,方法回调 记录节点,方便后续赋值
// 打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {
    NSLog(@"节点头%@",elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
}
// 3.节点的值根据节点内容决定值. 没有值时对应得结果\n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(nonnull NSString *)string {
//    sting是\n\t
    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
        NSLog(@"节点的值:%@----结束", string);
    }
    
}

//  4.遇到节点尾, 方法回调,判断当前节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName {
    NSLog(@"节点尾%@",elementName);
}
// 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
// NSLog(@"%@",)
}
// 解析失败掉用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败:%@",parseError);
}
// DOM解析
// 关于GDataXMLNode的引入
// 1.引入.h文件和.m文件
// 2.引入系统动态库libxml2.tbd
// 3.根据GDataXMLNode头文件的提示,添加    /usr/include/libxml2
// 4.如果是ARC环境下,需要对该文件进行MRC编译,添加-fno-objec-arc

- (IBAction)domParse:(id)sender {
    self.domStuArray = [NSMutableArray array];
   NSString *xmlDataPath =  [[NSBundle mainBundle]pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlDataPath];
    NSError *erro = nil;
//    1. 创建GDataXMLDocument对象
//    根据XML的Data数据生成一个树形的document
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&erro];
//    2.根据Document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
//    3.根据根节点获取子节点数组
    NSArray *rootChildElement = rootElement.children;
//    4.遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElement) {
//        NSLog(@"childElement name: %@",childElement.name);
//        5.对应所有student节点
        Student *stu = [Student new];
        [self.domStuArray addObject:stu];
//        6.遍历student 节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
//            7.赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@",self.domStuArray);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
